package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.activity.MVPlayActivity;
import com.lanou.moving.adapter.SearchMvAdapter;
import com.lanou.moving.entity.SearchMvRoot;
import com.lanou.moving.utils.MyConfig;
import com.lanou.moving.utils.NoScrollListView;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;


/**
 * Created by dllo on 15/10/23.
 */
public class MVFragment extends Fragment implements AdapterView.OnItemClickListener {
    private List<SearchMvRoot.DataEntity> datas;
    private String url;
    private VolleySingleton volleySingleton;
    private SearchMvAdapter adapter;
    private NoScrollListView noScrollListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_mv_page, null);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        noScrollListView = (NoScrollListView) view.findViewById(R.id.mv_list);
        noScrollListView.setOnItemClickListener(this);
        initData();
        return view;
    }

    private void initData() {
        //接收数据替换Q
        String q = MyConfig.getSharePreStr(getActivity(), "Searchcontent", "url");
        Log.i("heheh", "---" + q);
        url = "http://so.ard.iyyin.com/s/video?uid=866058021842951&f=f3040&app=ttpod&hid=1135212123029932&alf=alf203200&net=2&size=50&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&page=1&" +
                "q=" +
                q + "&imsi=460078098497828&tid=0";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchMvRoot mvRoot = gson.fromJson(response, SearchMvRoot.class);
                datas = mvRoot.getData();
                adapter = new SearchMvAdapter(getActivity(), datas);
                noScrollListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchMvRoot.DataEntity entity = (SearchMvRoot.DataEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(getActivity(), MVPlayActivity.class);
        intent.putExtra("SearchMvRoot", entity);
        intent.putExtra("mv_url", datas.get(position).getMvList().get(0).getUrl());
        intent.putExtra("videoId", datas.get(position).getMvList().get(0).getVideoId());
        startActivity(intent);
    }
}
